package br.com.foxline.atlasmobile;

import java.io.IOException;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.Toast;
import br.com.foxline.atlasmobile.adapter.ImageLoadTask;
import br.com.foxline.atlasmobile.dao.AnnexDao;
import br.com.foxline.atlasmobile.entities.Annex;
import br.com.foxline.atlasmobile.request.SynchronousHttpConnection;
import br.com.foxline.atlasmobile.touch.TouchImageView;
import br.com.foxline.atlasmobile.util.UtilConnection;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class FullImageActivity extends Activity {

//	private Long image = null;
	private boolean enviado = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.full_image);

		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearFullImage);

		Uri uri = getIntent().getData();

//		image = getIntent().getLongExtra("image", -1L);
		
		enviado = getIntent().getBooleanExtra("enviado", false);

		TouchImageView touchImageView = new TouchImageView(this);

		LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		touchImageView.setLayoutParams(lp);

		ImageLoadTask task = new ImageLoadTask(touchImageView, 500, 500, true);
		task.execute(uri);

		linearLayout.addView(touchImageView);
	}

	public void enviarAnexo() {
//		if (image != -1L && enviado) {
//			RequestWebJSON enviaranexo = new RequestWebJSON();
//			enviaranexo.execute(image);
//		}else if(!enviado){
//			Toast.makeText(getApplicationContext(), "Não é possivel enviar o anexo, pois o imóvel ainda não foi sincronizado", Toast.LENGTH_SHORT).show();
//		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getMenuInflater().inflate(R.menu.activity_menu_full, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;
		case R.id.sync_export:
//			System.err.println("ERRO"+image);
			enviarAnexo();
			break;
		}
		return true;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
		finish();
	}

	public class RequestWebJSON extends AsyncTask<Long, String, String> {

		private Context context;
		private ProgressDialog dialog;
		private AnnexDao annexDao;
		private String erros = "";

		private int cont;
		private int contAnnex;

		private SynchronousHttpConnection httpConnection;
		
		private String server;
		private String portNumber;
		private SharedPreferences prefs;
		
		private Gson gson;
		
		private RequestWebJSON() {
			super();

			this.context = context;
			this.annexDao = new AnnexDao(context);

			this.dialog = new ProgressDialog(context);
			this.dialog.setTitle("Enviando");
			this.dialog.setMessage("Conectando...");
			this.dialog.setCancelable(false);

			prefs = getApplicationContext()
					.getSharedPreferences(UtilConnection.APP_SHARED_PREFS, 0);

			server = prefs.getString(UtilConnection.SERVER,
					UtilConnection.SERVER_DEFAULT);
			portNumber = prefs.getString(UtilConnection.PORT_NUMBER,
					UtilConnection.PORT_NUMBER_DEFAULT);

			httpConnection = new SynchronousHttpConnection(
					server, portNumber);

			this.dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Fechar",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});

			this.dialog.show();

			prefs = this.context.getSharedPreferences(
					UtilConnection.APP_SHARED_PREFS, 0);
			server = prefs.getString(UtilConnection.SERVER,
					UtilConnection.SERVER_DEFAULT);
			portNumber = prefs.getString(UtilConnection.PORT_NUMBER,
					UtilConnection.PORT_NUMBER_DEFAULT);

		}

		@Override
		protected void onProgressUpdate(String... values) {
			super.onProgressUpdate(values);
			if (values.length > 0 && values[0] != null) {
				dialog.setMessage(values[0] + "" + erros);
			}

		}

		@Override
		protected String doInBackground(Long... param) {
			Annex annex = annexDao.find(param[0]);

			if (annex.getCod() == null) {
				SimpleDateFormat sdf = new SimpleDateFormat(
						"dd-MM-yyyy-HH-mm-ss");
				annex.setCod("#" + annex.getId() + "#" + annex.getUUID() + "#"
						+ sdf.format(annex.getCreate_at()));
			}
			annex.setId(null);
			annex.setImovel(null);

			Annex a1 = new Annex();
			a1.setCod(annex.getCod());
			a1.setContent(annex.getContent());
			annex.setContent(null);

			publishProgress("Enviando Anexo ");
			gson = new GsonBuilder().setDateFormat("dd/MM/yyyy").create();
			try {
				httpConnection.post(UtilConnection.URL_ASYNC,
						gson.toJson(a1));
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			return null;
		}

	}

}
